import 'package:flutter/material.dart';

import 'pages/about_us_page.dart';
import 'pages/home_page.dart';
import 'pages/news_page.dart';
import 'pages/product_page.dart';

class App extends StatefulWidget {
  const App({Key key}) : super(key: key);

  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  int _currentIndex = 0;
  PageController _pageController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _pageController = new PageController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("企业站点"),
      ),
      // 方案一
      // body: currentPage(),
      // 方案二
      body: buildPageView(),
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: _currentIndex,
          onTap: (value) {
            // 方案二 页面跳转
            _pageController.jumpToPage(value);
            setState(() {
              _currentIndex = value;
            });
          },
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
            BottomNavigationBarItem(
                icon: Icon(Icons.poll_rounded), label: "产品"),
            BottomNavigationBarItem(
                icon: Icon(Icons.fiber_new_sharp), label: "新闻"),
            BottomNavigationBarItem(icon: Icon(Icons.message), label: "关于我们"),
          ]),
    );
  }

  // 方案一
  AboutUsPage aboutUsPage;
  HomePage homePage;
  NewsPage newsPage;
  ProductPage productPage;
  currentPage() {
    switch (_currentIndex) {
      case 0:
        if (homePage == null) {
          homePage = HomePage();
        }
        return homePage;
        break;
      case 1:
        if (productPage == null) {
          productPage = ProductPage();
        }
        return productPage;
        break;
      case 2:
        if (newsPage == null) {
          newsPage = NewsPage();
        }
        return newsPage;
        break;
      case 3:
        if (aboutUsPage == null) {
          aboutUsPage = AboutUsPage();
        }
        return aboutUsPage;
        break;
      default:
    }
  }

  // 方案二

  buildPageView() {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: PageView(
        controller: _pageController,
        physics: NeverScrollableScrollPhysics(),
        children: [
          // 首页
          HomePage(),
          // 产品页面
          ProductPage(),
          // 新闻页面
          NewsPage(),
          // 关于我们
          AboutUsPage(),
        ],
      ),
    );
  }
}
